package models

import (
	"errors"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	_ "github.com/go-sql-driver/mysql"
)

type Login struct {
	Id       int `orm:"auto"`
	Username string
	Password string
}

func init() {
	orm.RegisterModel(new(Login))
	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/table")
	orm.RunSyncdb("default", false, true)
	orm.Debug = true
}

func Log(username string) (Login, error) {
	o := orm.NewOrm()
	l := Login{Username: username}

	err := o.Read(&l, "username")
	fmt.Println(err)
	if err != nil {
		if err == orm.ErrNoRows {
			return Login{}, errors.New("用户名不存在")
		}
	}
	return l, nil
}
